/**
 * @description 房源信息增删改查 api 路由
 * @date 2020-08-13 15:05
 * @auth LI
 */

const router = require('koa-router')()
const {
  addHouseInfo,
  updateHouseInfo,
  getHouseInfos,
  deleteHouseInfo
} = require('../../controller/houseInfo')

router.prefix('/jihuide/houseInfo')

// 查询房源信息
router.post('/getHouseInfos', async (ctx, next) => {
  let {
    houseOwnerId,
    supervisionId,
    foremanId,
    bricklayerId,
    carpenterId,
    plumberId,
    painterId,
    houseName,
    page,
    limit
  } = ctx.request.body
  if (houseOwnerId) {
    houseOwnerId = parseInt(houseOwnerId)
  }
  if (supervisionId) {
    supervisionId = parseInt(supervisionId)
  }
  if (foremanId) {
    foremanId = parseInt(foremanId)
  }
  if (bricklayerId) {
    bricklayerId = parseInt(bricklayerId)
  }
  if (carpenterId) {
    carpenterId = parseInt(carpenterId)
  }
  if (plumberId) {
    plumberId = parseInt(plumberId)
  }
  if (painterId) {
    painterId = parseInt(painterId)
  }

  // 接下来交给控制层
  ctx.body = await getHouseInfos({
    houseOwnerId,
    supervisionId,
    foremanId,
    bricklayerId,
    carpenterId,
    plumberId,
    painterId,
    houseName,
    page,
    limit
  })
})

// 添加房源信息
router.post('/addHouseInfo', async (ctx, next) => {
  let {
    houseName,
    houseAddress,
    roomType,
    startTime,
    drawing,
    houseOwnerId,
    supervisionId,
    foremanId,
    bricklayerId,
    carpenterId,
    plumberId,
    painterId,
    remark
  } = ctx.request.body
  
  houseOwnerId = parseInt(houseOwnerId)
  supervisionId = parseInt(supervisionId)
  foremanId = parseInt(foremanId)
  bricklayerId = parseInt(bricklayerId)
  carpenterId = parseInt(carpenterId)
  plumberId = parseInt(plumberId)
  painterId = parseInt(painterId)
  // 接下来交给控制层
  ctx.body = await addHouseInfo({
    houseName,
    houseAddress,
    roomType,
    startTime,
    drawing,
    houseOwnerId,
    supervisionId,
    foremanId,
    bricklayerId,
    carpenterId,
    plumberId,
    painterId,
    remark
  })
})

// 修改房源信息
router.post('/updateHouseInfo', async (ctx, next) => {
  let {
    id,
    houseName,
    houseAddress,
    roomType,
    startTime,
    drawing,
    status,
    houseOwnerId,
    supervisionId,
    foremanId,
    bricklayerId,
    carpenterId,
    plumberId,
    painterId,
    remark
  } = ctx.request.body
  id = parseInt(id)
  status = parseInt(status)
  houseOwnerId = parseInt(houseOwnerId)
  supervisionId = parseInt(supervisionId)
  foremanId = parseInt(foremanId)
  bricklayerId = parseInt(bricklayerId)
  carpenterId = parseInt(carpenterId)
  plumberId = parseInt(plumberId)
  painterId = parseInt(painterId)
  // 接下来交给控制层
  ctx.body = await updateHouseInfo({
    id,
    houseName,
    houseAddress,
    roomType,
    startTime,
    drawing,
    status,
    houseOwnerId,
    supervisionId,
    foremanId,
    bricklayerId,
    carpenterId,
    plumberId,
    painterId,
    remark
  })
})

// 删除房源信息
router.post('/deleteHouseInfo', async (ctx, next) => {
  let { id } = ctx.request.body
  id = parseInt(id)
  // 接下来交给控制层
  ctx.body = await deleteHouseInfo(id)
})

module.exports = router
